package com.wangp.demo.anno.util;

/**
 * @author <a href="mailto:wangpeng@i-baby.com" rel="nofollow">wp☠😘✨</a>
 * @version 0.0.1
 * @since 2025/8/31 19:22
 */
public class StringUtils {

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.isEmpty();
    }

    public static boolean isNotEmpty(CharSequence str) {
        return !isEmpty(str);
    }

    public static String removePrefix(String str, String prefix) {
        if (!isEmpty(str) && !isEmpty(prefix)) {
            return str.startsWith(prefix) ? subSuf(str, prefix.length()) : str;
        } else {
            return str;
        }
    }

    public static String subSuf(String string, int fromIndex) {
        return isEmpty(string) ? null : sub(string, fromIndex, string.length());
    }

    public static String sub(String str, int fromIndexInclude, int toIndexExclude) {
        if (isEmpty(str)) {
            return str;
        } else {
            int len = str.length();
            if (fromIndexInclude < 0) {
                fromIndexInclude += len;
                if (fromIndexInclude < 0) {
                    fromIndexInclude = 0;
                }
            } else if (fromIndexInclude > len) {
                fromIndexInclude = len;
            }
            if (toIndexExclude < 0) {
                toIndexExclude += len;
                if (toIndexExclude < 0) {
                    toIndexExclude = len;
                }
            } else if (toIndexExclude > len) {
                toIndexExclude = len;
            }
            if (toIndexExclude < fromIndexInclude) {
                int tmp = fromIndexInclude;
                fromIndexInclude = toIndexExclude;
                toIndexExclude = tmp;
            }
            return fromIndexInclude == toIndexExclude ? "" : str.substring(fromIndexInclude, toIndexExclude);
        }
    }

    public static String lowerFirst(String str) {
        if (null == str) {
            return null;
        } else {
            if (!str.isEmpty()) {
                char firstChar = str.charAt(0);
                if (Character.isUpperCase(firstChar)) {
                    return Character.toLowerCase(firstChar) + subSuf(str, 1);
                }
            }

            return str;
        }
    }

    public static String upperFirst(String str) {
        if (null == str) {
            return null;
        } else {
            if (!str.isEmpty()) {
                char firstChar = str.charAt(0);
                if (Character.isLowerCase(firstChar)) {
                    return Character.toUpperCase(firstChar) + subSuf(str, 1);
                }
            }

            return str;
        }
    }
}
